package com.renrenyou.duanshipin.base;

import android.content.Context;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

    private List<T> mData = new ArrayList<>();
    private LayoutInflater mInflater;

    public BaseAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void refreshData(List<T> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    public void loadMoreData(List<T> data) {
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return mData;
    }

    public LayoutInflater getInflater() {
        return mInflater;
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        bindHolder(holder, position, mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    abstract public void bindHolder(VH holder, int position, T data);

}
